﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using automobili_data;
using automobili_data.interfaces;
using System.Net.Mail;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace automobili_gui.UserManagement
{
    public partial class Pretraga : System.Web.UI.Page
    {
        ArrayList mod, mar, gor, kar;
        String mark = null, mode = null, goriv = null, karoserij = null;
        int skuplj=0, jeftinij=0, starij=0, mladj=0;
        double cena;
        int godina;
        int provera = 0;
        User k = null;
        Privilegije p = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            p = SessionUtils.getCurrentPrivileges();
            if (p == null) { p = AutomobiliDataManager.instance.dohvatiPodrazPrivilegije(); }
            if (!Page.IsPostBack && p.Pretraga_s==1) {
                dohvatiModele();
                dohvatiMarke();
                dohvatiGorivo();
                dohvatiKaroseriju();
                k = SessionUtils.getCurrentUser();
            }
        }
        private void dohvatiModele()
        {
            mod = AutomobiliDataManager.instance.dohvatiModele();
            mod.Insert(0,"");
            modelLista.DataSource = mod;
            modelLista.DataBind();

        }

        private void dohvatiMarke() {
            mar = AutomobiliDataManager.instance.dohvatiMarke();
            mar.Insert(0,"");
            markaLista.DataSource = mar;
            markaLista.DataBind();
        }

        private void dohvatiGorivo() {
            gor = AutomobiliDataManager.instance.dohvatiGorivo();
            gor.Insert(0, "");
            gorivoLista.DataSource = gor;
            gorivoLista.DataBind();

        }

        private void dohvatiKaroseriju() {
            kar = AutomobiliDataManager.instance.dohvatiKaroseriju();
            kar.Insert(0, "");
            karoserijaLista.DataSource = kar;
            karoserijaLista.DataBind();
            
        }

        protected void trazi(object sender, EventArgs e) 
        {
            if (godinaUnos.Text !="") {
                try { godina = Int32.Parse(godinaUnos.Text);
                if (godina < 1900)
                {
                    godP.Text = "Godina proizvodnje ne sme biti manja od 1900!";
                    provera = 1;
                }
                }
                catch { godP.Text = "Greska, morate uneti broj";
                provera = 1;
                }
                
            }
            

            if (cenaUnos.Text !="") {
                try
                {
                    cena = Double.Parse(cenaUnos.Text);
                    if (cena < 0)
                    {
                        cen.Text = "Cena ne sme biti negativan broj!";
                        provera = 1;
                    }
                }
                catch { cen.Text = "Greska, morate uneti broj";
                provera = 1;
                }
            }

            if (provera == 0)
            {
                if (markaLista.SelectedValue != "")  mark = markaLista.SelectedItem.Value; 
                if (modelLista.SelectedValue != "") mode = modelLista.SelectedItem.Value;
                if (gorivoLista.SelectedValue != "") goriv = gorivoLista.SelectedItem.Value;
                if (karoserijaLista.SelectedValue != "") karoserij = karoserijaLista.SelectedItem.Value;
                if (Skuplji.Checked) { skuplj = 1; }
                if (Jeftiniji.Checked) { jeftinij = 1; }
                if (Stariji.Checked) { starij = 1; }
                if (Mladji.Checked) { mladj = 1; }

                IList<Automobil> automobili = AutomobiliDataManager.instance.nadjiAutomobileDeo(mark, mode, karoserij, goriv, cena, godina, skuplj, jeftinij, starij, mladj);


                GridView1.DataSource = automobili;

                GridView1.DataBind();
            }


        }


        private void SendEMail (MailMessage mailMessage)
        {
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("automobil.portal@gmail.com", "iep_2013");
            client.Host = "smtp.gmail.com";

            try
            {
                client.Send(mailMessage);
            }
            catch (SmtpException)
            {
                throw;
            }
        }

        public void Send (int idR, int idO)
        {
            MailMessage Message = new MailMessage();
            MailAddress Email = new MailAddress("jeca_dk@yahoo.com");
            MailAddress Email1 = new MailAddress("automobil.portal@gmail.com");
            Message.To.Add(Email);
            Message.To.Add(Email1);
            //Message.From = new MailAddress(k.UserName);
            Message.From = new MailAddress("automobil.portal@gmail.com");
            Message.Subject = "Id rezervacije " + idR + ". Id oglasa " + idO;
            Message.Body = "Id vase rezervacije je: " + idR + ". Rezervisali ste automobil sa brojem oglasa: " + idO;
            Attachment Attachment = new Attachment(@"D:\\prvi"+k.UserName+".pdf");
            Attachment.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            Message.Attachments.Add(Attachment);
            SendEMail(Message);
        }
        protected void Prikazi_sledece(object sender, GridViewPageEventArgs e)
        {

            GridView1.PageIndex = e.NewPageIndex;

        //    DataBind();

        }

      


        public int detalji {
            get {
                return Int32.Parse(GridView1.SelectedRow.Cells[1].Text);
            }
        }

        protected void Prikazi(object sender, EventArgs e)
        {
            DataBind();
        }



        //protected void rezervisi (object sender, GridViewCommandEventArgs e)
        //{
        //    if (e.CommandName == "rezervisi")
        //    {
        //        k = SessionUtils.getCurrentUser();
        //        GridView1.SelectRow(1);
        //        int index = Convert.ToInt32(e.CommandArgument);
        //        int idAu = Int32.Parse(GridView1.Rows[index].Cells[0].Text);
      
        //        AutomobiliDataManager.instance.kreirajRezervaciju(k.UserId, idAu);
        //        IList<Rezervacija> rezerv = AutomobiliDataManager.instance.podaciZaRezervaciju();
        //        int idRez = rezerv[0].IdRez;
        //        int idOgl = rezerv[0].IdOgl;
        //        //
        //        generatePDFTest();
        //        this.Send(idRez, idOgl);
        //        Label1.Text = idAu.ToString();
        //    }
        //}

        static PdfPTable napraviTabelu ()
        {
            PdfPTable pdft = new PdfPTable(6);
            PdfPCell cell = new PdfPCell(new Phrase("Proba"));
            pdft.AddCell(cell);
            return pdft;
        }

        static void addHeaderTable (Document d)
        {
            PdfPTable header = new PdfPTable(3);
            header.WidthPercentage = (100);

            header.DefaultCell.BackgroundColor = BaseColor.BLACK;
            Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.WHITE);
            Phrase p = new Phrase("Foobar Film Festival", font);
            header.AddCell(p);

            header.DefaultCell.HorizontalAlignment = (Element.ALIGN_CENTER);
            p = new Phrase("2013/05/15", font);
            header.AddCell(p);

            header.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
            p = new Phrase("page 1", font);
            header.AddCell(p);

            d.Add(header);
            d.Add(new Paragraph());
        }

        public void generatePDFTest ()
        {
            // step 1
            Document document = new Document();
            // step 2
            PdfWriter.GetInstance(document,
                new System.IO.FileStream("D:\\prvi" + k.UserName + ".pdf", System.IO.FileMode.Create));

            // step 3
            document.Open();


            addHeaderTable(document);
            // step 4
            document.Add(napraviTabelu());


            // step 5
            document.Close();
        }

       

       
   


      

      

       

        

     

     

      
      
     
      

     

    }
}